package org.nbile.component;

import org.stellar.sdk.*;
import org.stellar.sdk.responses.AccountResponse;
import org.stellar.sdk.responses.SubmitTransactionResponse;

/**
 * Document Start
 * Anchor操作工具类
 * Document End
 * Author: songwenpeng@le.com
 * Time: 2016年9月27日 上午12:13:52
 */
public class AnchorComponent {

    /**
     * Document Start
     * 设置信任某一个资产
     * Document End
     * Author: songwenpeng@le.com
     * Time: 2016年10月27日 下午12:00:12
     * @param issuerCode      发行账户货币代码
     * @param issuerAccountID 发行账户ID
     * @param accountSeed     添加信任资产账户Seed
     * @param limit           信任线限额
     * @return
     */
    public SubmitTransactionResponse setTrust(Server server, String issuerCode, String issuerAccountID, String accountSeed, String limit) {
        //创建源 目标 账户对象
        KeyPair account = KeyPair.fromSecretSeed(accountSeed);
        KeyPair issuer = KeyPair.fromAccountId(issuerAccountID);
        try {

            // If there was no error, load up-to-date information on your account.
            AccountResponse accountResponse = server.accounts().account(account);

            Asset asset = AssetTypeCreditAlphaNum4.createNonNativeAsset(issuerCode, issuer);

            // CreateOperation 创建一个账户 并设置初始化保证金金额
            Transaction transaction = new Transaction.Builder(accountResponse).addOperation(new ChangeTrustOperation.Builder(asset, limit).build())
                    // A memo allows you to add your own metadata to a transaction. It's
                    // optional and does not affect how Stellar treats the transaction.
                    .addMemo(Memo.text("Issuering Assert")).build();
            // Sign the transaction to prove you are actually the person sending it.
            transaction.sign(account);
            // And finally, send it off to Stellar!
            SubmitTransactionResponse response = server.submitTransaction(transaction);
            return response;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Document Start
     * 资产转账交易
     * 仅仅支持四位编码的资产代码
     * Document End
     * Author: songwenpeng@le.com
     * Time: 2016年10月27日 下午4:29:51
     * @param sourceSeed      转出地址seed
     * @param targetAccountID 目标AccountID
     * @param amount          金额
     * @param issuerCode      资产代码
     * @param issuerAccountID 资产发行账户
     * @return
     */
    public SubmitTransactionResponse transfer(Server server, String sourceSeed, String targetAccountID, String amount, String issuerCode, String issuerAccountID) {
        //创建源 目标 账户对象
        KeyPair source = KeyPair.fromSecretSeed(sourceSeed);
        KeyPair destination = KeyPair.fromAccountId(targetAccountID);
        KeyPair issuer = KeyPair.fromAccountId(issuerAccountID);

        try {
            // First, check to make sure that the destination account exists.
            // You could skip this, but if the account does not exist, you will be charged
            // the transaction fee when the transaction fails.
            // It will throw HttpResponseException if account does not exist or there was another error.
            server.accounts().account(destination);

            // If there was no error, load up-to-date information on your account.
            AccountResponse sourceAccount = server.accounts().account(source);

            Asset asset = AssetTypeCreditAlphaNum4.createNonNativeAsset(issuerCode, issuer);

            // Start building the transaction.
            Transaction transaction = new Transaction.Builder(sourceAccount).addOperation(new PaymentOperation.Builder(destination, asset, amount).build())
                    // A memo allows you to add your own metadata to a transaction. It's
                    // optional and does not affect how Stellar treats the transaction.
                    .addMemo(Memo.text("Tranfer" + issuerCode)).build();
            // Sign the transaction to prove you are actually the person sending it.
            transaction.sign(source);
            // And finally, send it off to Stellar!
            SubmitTransactionResponse response = server.submitTransaction(transaction);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * AllowTrust
     * 允许添加信用
     * 仅仅支持四位编码的资产代码
     * @param server
     * @param sourceSeed
     * @param targetAccountID
     * @param issuerCode
     * @param issuerAccountID
     * @return
     */
    public SubmitTransactionResponse allowTrust(Server server, String sourceSeed, String targetAccountID, String issuerCode, String issuerAccountID, boolean isAllowTrust) {
        //创建源 目标 账户对象
        KeyPair source = KeyPair.fromSecretSeed(sourceSeed);
        KeyPair destination = KeyPair.fromAccountId(targetAccountID);
        KeyPair issuer = KeyPair.fromAccountId(issuerAccountID);

        try {
            // First, check to make sure that the destination account exists.
            // You could skip this, but if the account does not exist, you will be charged
            // the transaction fee when the transaction fails.
            // It will throw HttpResponseException if account does not exist or there was another error.
            server.accounts().account(destination);

            // If there was no error, load up-to-date information on your account.
            AccountResponse sourceAccount = server.accounts().account(source);

            Asset asset = AssetTypeCreditAlphaNum4.createNonNativeAsset(issuerCode, issuer);

            // Start building the transaction.
            Transaction transaction = new Transaction.Builder(sourceAccount).addOperation(new AllowTrustOperation.Builder(destination, issuerCode, isAllowTrust).build()).build();
            // Sign the transaction to prove you are actually the person sending it.
            transaction.sign(source);
            // And finally, send it off to Stellar!
            SubmitTransactionResponse response = server.submitTransaction(transaction);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
